/**
 *	@file	pgear_map.c
 *	@brief	ポケギア　システムマップ
 *	@author	Miyuki Iwasawa
 *	@date	07.06.20
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/brightness.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/wipe.h"
#include "system/buflen.h"
#include "gflib/touchpanel.h"
#include "field/field_matrix.h"
#include "field/field_place_name.h"
#include "savedata/encount.h"
#include "application/app_tool.h"
#include "application/pokegear.h"
#include "msgdata/msg.naix"
#include "fielddata/maptable/zone_id.h"

#include "../pgear_sys.h"
#include "../pgear_frm.h"
#include "../pgear_act.h"
#include "pgmap_sys.h"
#include "pgmap_bg.h"
#include "pgmap_sub.h"
#include "pgmap_key.h"
#include "pgmap_mk.h"
#include "msgdata/msg_pgmap_guide.h"

//データテーブル
#include "dat/mzone_param.dat"
#include "dat/mtown_param.dat"

#include "system/pm_overlay.h"
FS_EXTERN_OVERLAY(field_revenge);

//プロトタイプ宣言　グローバル
//================================================================
///オーバーレイプロセス定義
PROC_RESULT PGearMapProc_Init( PROC *proc,int *seq);
PROC_RESULT PGearMapProc_Main( PROC *proc,int *seq);
PROC_RESULT PGearMapProc_End( PROC *proc,int *seq);

//プロトタイプ宣言　ローカル
//================================================================
static void mapsub_InitDataSet(PGMAP_SYS_WORK* wk);
static void mapsub_LastDataSet(PGMAP_SYS_WORK* wk);
static int mapseq_AppInit(PGMAP_SYS_WORK* wk);
static int mapseq_AppMain(PGMAP_SYS_WORK* wk);
static int mapseq_AppEnd(PGMAP_SYS_WORK* wk);
static int mapseq_MarkInit(PGMAP_SYS_WORK* wk);
static int mapseq_MarkMain(PGMAP_SYS_WORK* wk);
static int mapseq_MarkEndMap(PGMAP_SYS_WORK* wk);
static int mapseq_MarkEndWord(PGMAP_SYS_WORK* wk);
static int mapseq_AllFadeIn(PGMAP_SYS_WORK* wk);
static int mapseq_AllFadeOut(PGMAP_SYS_WORK* wk);
static int mapseq_ViewFadeIn(PGMAP_SYS_WORK* wk);
static int mapseq_ViewFadeOut(PGMAP_SYS_WORK* wk);

/**
 *	@brief	ポケギア マッププロセス初期化
 *	@param	proc	プロセスデータ
 *	@param	seq		シーケンス
 *
 *	@return	処理状況
 */
PROC_RESULT PGearMapProc_Init( PROC *proc,int *seq)
{
	PGMAP_SYS_WORK *wk = NULL;
	PGEAR_SYS_WORK * sys_wk = (PGEAR_SYS_WORK*)PROC_GetParentWork(proc);

	//ワークエリア取得
	HeapStatePush();

	Overlay_Load( FS_OVERLAY_ID( field_revenge ), OVERLAY_LOAD_NOT_SYNCHRONIZE );

	//ヒープ作成
	sys_CreateHeap(HEAPID_BASE_APP,HEAPID_PGEAR_APP,0x30000);

	wk = PROC_AllocWork( proc,sizeof(PGMAP_SYS_WORK),HEAPID_PGEAR_APP);
	memset(wk,0,sizeof(PGMAP_SYS_WORK));

	//パラメータ引継ぎ
	wk->sys = sys_wk;
	wk->heapID = HEAPID_PGEAR_APP;

	//引継ぎデータの初期化
	mapsub_InitDataSet(wk);

	//サウンド引継ぎ設定
//	Snd_DataSetByScene( SND_SCENE_SUB_TOWNMAP, 0, 0 );

	wk->pMZoneTbl = PGearMap_MapZoneParam;
	wk->zTblMax = PGMAP_ZONE_MAX;
	return PROC_RES_FINISH;
}

/**
 *	@brief	ポケギア　マッププロセスメイン
 *	@param	proc	プロセスデータ
 *	@param	seq		シーケンス
 *
 *	@return 処理状況
 */
PROC_RESULT PGearMapProc_Main(PROC* proc,int *seq)
{
	PGMAP_SYS_WORK* wk = PROC_GetWork(proc);

	switch(*seq){
	case PGMAP_SEQ_INIT:
		*seq = mapseq_AppInit(wk);
		break;
	case PGMAP_SEQ_MAIN:
		*seq = mapseq_AppMain(wk);
		break;
	case PGMAP_SEQ_END:
		*seq = mapseq_AppEnd(wk);
		break;
	case PGMAP_SEQ_ALL_FADEIN:
		*seq = mapseq_AllFadeIn(wk);
		break;
	case PGMAP_SEQ_ALL_FADEOUT:
		*seq = mapseq_AllFadeOut(wk);
		break;
	case PGMAP_SEQ_VIEW_FADEIN:
		*seq = mapseq_ViewFadeIn(wk);
		break;
	case PGMAP_SEQ_VIEW_FADEOUT:
		*seq = mapseq_ViewFadeOut(wk);
		break;
	case PGMAP_SEQ_MARK_INIT:
		*seq = mapseq_MarkInit(wk);
		break;
	case PGMAP_SEQ_MARK_MAIN:
		*seq = mapseq_MarkMain(wk);
		break;
	case PGMAP_SEQ_MARK_END_MAP:
		*seq = mapseq_MarkEndMap(wk);
		break;
	case PGMAP_SEQ_MARK_END_WORD:
		*seq = mapseq_MarkEndWord(wk);
		break;
	case PGMAP_SEQ_APP_EXIT:
		return PROC_RES_FINISH;
	}
	return PROC_RES_CONTINUE;
}

/**
 *	@brief	ポケギア　メインプロセス終了
 *
 *	@param	proc	プロセスデータ
 *	@param	seq		シーケンス
 *
 *	@return 処理状況
 */
PROC_RESULT PGearMapProc_End(PROC* proc,int* seq)
{
	int heapID = 0;
	PGMAP_SYS_WORK* wk = PROC_GetWork(proc);

	//セーブデータ反映
	mapsub_LastDataSet(wk);

	//ゾーンデータ解放
	FreeFMZ(wk->pZone);

	if(wk->sys->next_mode != PG_NEXT_WORD){
		//アプリケーション初回呼び出し終了フラグを立てておく
		wk->sys->call_end = 1;
	}

	//ワークエリア開放
	heapID = wk->heapID;
	PROC_FreeWork(proc);

	HeapStatePop();
	HeapStateCheck(heapID);
	sys_DeleteHeap(heapID);

	Overlay_UnloadID( FS_OVERLAY_ID( field_revenge ) );

	return PROC_RES_FINISH;
}

/**
 *	@brief	シント遺跡チェック
 */
static BOOL check_Shinto(u16 player_zone)
{
	int i;
	const u16 shinto_zone[] = {
	 ZONE_ID_D51R0101,	//0
	 ZONE_ID_D51R0201,	//1
	 ZONE_ID_D51R0301,	//2
	};

	for(i = 0;i < NELEMS(shinto_zone);i++){
		if(player_zone == shinto_zone[i]){
			return TRUE;
		}
	}
	return FALSE;
}

/**
 *	@brief	アクア号チェック
 */
static BOOL check_Akua(u16 player_zone)
{
	int i;
	const u16 akua_zone[] = {
	 ZONE_ID_P01R0301,	//0
	 ZONE_ID_P01R0302,	//1
	 ZONE_ID_P01R0303,	//2
	 ZONE_ID_P01R0304,	//3
	 ZONE_ID_P01R0305,	//4
	 ZONE_ID_P01R0306,	//5
	 ZONE_ID_P01R0307,	//6
	};

	for(i = 0;i < NELEMS(akua_zone);i++){
		if(player_zone == akua_zone[i]){
			return TRUE;
		}
	}
	return FALSE;
}

/**
 *	@brief	ポケギアマップ　初期データ設定
 */
static void mapsub_InitDataSet(PGMAP_SYS_WORK* wk)
{
	static const u16 scrLimBlock[]={
		MAPS_LIM_BR0,MAPS_LIM_BR1,MAPS_LIM_BR2,
	};

	//システムにアプリワークポインタを通知
	wk->sys->app_vwork = (void*)wk;
	//システムにSysKey2AppKey描画の切替関数ポインタを通知
	wk->sys->to_akey_func = PGMapSub_ToKeyChangeMap;
	wk->sys->to_touch_func = PGMapSub_ToTouchChangeMap;

	//マーカーセーブデータを取得
	PGMapMk_GetSaveData(wk);

	//初期呼び出しモード設定
	if(wk->sys->mode != PGAPP_MODE_MAP_MARK){
		wk->call_mode = PGMAP_CALL_MAP;
		wk->sys->mode = PGAPP_MODE_MAP;

		//セーブデータから
		wk->f_affine = PGearSv_GetMapAff(wk->sys->pg_save);
	}else{
		wk->call_mode = PGMAP_CALL_MARK;

		//sys->ret構造体に保存されていたパラメータを復帰
		wk->ret = wk->sys->ret.map;
		wk->f_affine = wk->ret.affine;

		//新しいワードが登録されていれば反映
		if(wk->ret.word != PMS_WORD_NULL){
			MARKER_LIST* pl = PGMapMk_SearchListFromZone(wk,wk->ret.zone);
			if(pl == NULL){
				pl = PGMapMk_EntryZoneData(wk,wk->ret.zone);
			}
			PGMapMk_AddWordParam(pl,wk->ret.word_pos,wk->ret.word);
		}
		//リターンパラメータ有効フラグを落とす
		wk->sys->ret.valid = FALSE;
		wk->sys->mode = PGAPP_MODE_MAP;
	}

	wk->keymode = 0;
	wk->keymode_st = 0;
	wk->f_open = PGearSv_GetMapOpenLevel(wk->sys->pg_save);

	//ゾーンリソース取得
	wk->pZone = InitFMZ(wk->heapID);
	//電話セーブデータ取得
	wk->tel_sv = SaveData_GetPMTelSave(wk->sys->save);
	//電話帳データ取得
	wk->tel_book = PMTelBookData_AllocAll(wk->heapID);
	wk->tel_number = PGearSv_GetTelBook(wk->sys->pg_save,wk->heapID);
	//登録数取得
	wk->tel_nummax = PGearSv_GetTelBookEntryNum(wk->sys->pg_save);

	//自機の初期位置
#if 0
	wk->player_org.x = wk->sys->param->player_x/MBLOCK_GRID;
	wk->player_org.z = wk->sys->param->player_z/MBLOCK_GRID;
	wk->player.x = wk->player_org.x;
	wk->player.z = wk->player_org.z+PGMAP_MATOFS_Z;
	wk->player.zone = wk->sys->param->player_zone;
	wk->player.val = wk->sys->param->player_sex;
#else
	wk->player.x = wk->sys->param->tmap_gx;
	wk->player.z = wk->sys->param->tmap_gz+PGMAP_MATOFS_Z;
	wk->player.zone = wk->sys->param->player_zone;
	wk->player.val = wk->sys->param->player_sex;
#endif

	//ターゲットの初期ブロック位置
	wk->cTarget.x = wk->sys->param->tmap_gx;	//wk->player.x;
	wk->cTarget.z = wk->sys->param->tmap_gz+PGMAP_MATOFS_Z;	//wk->player.z;

	//スクロールリミット設定
	wk->scrl.lim_bl = MAPS_LIM_BL;
	wk->scrl.lim_bu = MAPS_LIM_BU;
	wk->scrl.lim_br = scrLimBlock[wk->f_open];
	wk->scrl.lim_bd = MAPS_LIM_BD;

	//マップフィールドのオフセット値登録
	wk->mapf_usiz = MAPF_U_SIZ;
	wk->mapf_lsiz = MAPF_L_SIZ;
	wk->maps_hdef = PGM_MAPS_HDEF;

	//サファリ他の到着フラグ取得
	wk->arrive_safari = TRUE;	//SysFlag_ArriveGet(wk->sys->ev_save,FLAG_ARRIVE_R47); //強制オープン090629
	wk->arrive_t24 = SysFlag_ArriveGet(wk->sys->ev_save,FLAG_ARRIVE_T24);

	//シント
	wk->arrive_shinto = check_Shinto(wk->player.zone);
	//アクア
	wk->arrive_ship = check_Akua(wk->player.zone);

	//移動ポケモンデータ取得
	{
		int i;
		ENC_SV_PTR enc_sv = EncDataSave_GetSaveDataPtr(wk->sys->save);
		for(i = 0;i < MOVE_POKE_MAX;i++){
			MPD_PTR mpd;

			if(EncDataSave_IsMovePokeValid(enc_sv, i)){
				mpd = EncDataSave_GetMovePokeDataPtr(enc_sv, i);
				wk->mv_poke_zone[i] = EncDataSave_GetMovePokeDataParam(mpd, MP_PARAM_ZONE_ID);
			}else{
				wk->mv_poke_zone[i] = 0;
			}
		}
#ifdef DEBUG_ONLY_FOR_iwasawa
#if 0
		wk->mv_poke_zone[0] = ZONE_ID_R30;
		wk->mv_poke_zone[1] = ZONE_ID_R33;
		wk->mv_poke_zone[2] = ZONE_ID_R35;
		wk->mv_poke_zone[3] = ZONE_ID_W40;
#endif
#endif
	}
}

/**
 *	@brief	ポケギアマップ　終了時データ書き戻し設定
 */
static void mapsub_LastDataSet(PGMAP_SYS_WORK* wk)
{
	sys_FreeMemoryEz(wk->tel_number);
	PMTelBookData_Free(wk->tel_book);

	//関数ポインタクリア
	wk->sys->to_akey_func = NULL;
	wk->sys->to_touch_func = NULL;

	//マーカーデータ書き戻し
	PGMapMk_SetSaveData(wk);

	//マップの拡縮状態書き戻し
	PGearSv_SetMapAff(wk->sys->pg_save,wk->f_affine);
}

/**
 *	@brief	ポケギアマップ　シーケンス初期化
 */
static int mapseq_AppInit(PGMAP_SYS_WORK* wk)
{
	if(!PGMap_AppInit(wk)){
		return PGMAP_SEQ_INIT;
	}

	if(wk->sys->call_end){
		return PGMAP_SEQ_VIEW_FADEIN;
	}else{
		return PGMAP_SEQ_ALL_FADEIN;
	}
}

/**
 *	@brief	ポケギアマップ　メインシーケンス
 */
static int mapseq_AppMain(PGMAP_SYS_WORK* wk)
{
	int ret = PG_NEXT_NULL;
	BOOL touch_f = FALSE;
	int zone;

	ret = PGMapKey_InputTouch(wk,&touch_f);

	if(!touch_f){
		PGFrame_ChangeKeyModeCheck(wk->sys);

		if(wk->sys->control == PGCONT_SYS){
			ret = PGFrame_SysKeyControl(wk->sys);
		}else{
			ret = PGMapKey_InputKey(wk);
		}
	}else{
		wk->sys->key_mode == APP_KTST_TOUCH;
	}

	wk->sys->next_mode = ret;
	switch(ret){
	case PG_NEXT_NULL:
		break;
	case PG_NEXT_MARK:
		return PGMAP_SEQ_MARK_INIT;
	case PG_NEXT_EXIT:
		return PGMAP_SEQ_ALL_FADEOUT;
	default:
		//アプリ切替
		return PGMAP_SEQ_VIEW_FADEOUT;
	}
	return PGMAP_SEQ_MAIN;
}

/**
 *	@brief	ポケギアマップ　シーケンス終了
 */
static int mapseq_AppEnd(PGMAP_SYS_WORK* wk)
{
	if(!PGMap_AppEnd(wk)){
		return PGMAP_SEQ_END;
	}

	//リターンパラメータをシステムに返す
	wk->sys->ret.map = wk->ret;
	return PGMAP_SEQ_APP_EXIT;
}

/**
 *	@brief	ポケギアマップ　マーク画面初期化
 */
static int mapseq_MarkInit(PGMAP_SYS_WORK* wk)
{
	if(!PGMap_MarkInit(wk)){
		return PGMAP_SEQ_MARK_INIT;
	}

	return PGMAP_SEQ_MARK_MAIN;
}

/**
 *	@brief	ポケギアマップ　マーク画面メイン
 */
static int mapseq_MarkMain(PGMAP_SYS_WORK* wk)
{
	int ret = -1;
	int old_mode;
	BOOL touch_f = FALSE;

	switch(wk->mkedit_st){
	case MKEDIT_SEL:
		ret = PGMapKey_ContMEditTSelectTouch(wk,&touch_f);
		break;
	case MKEDIT_MSEL:
		if(wk->sys->key_mode == APP_KTST_KEY){
			break;
		}
		ret = PGMapKey_ContMEditMSelectTouch(wk);
		touch_f = TRUE;
		break;
	case MKEDIT_DEL:
		if(wk->sys->key_mode == APP_KTST_KEY){
			break;
		}
		ret = PGMapKey_ContMEditTDeleteTouch(wk);
		touch_f = TRUE;
		break;
	}
	if(touch_f){
		if(wk->sys->key_mode != APP_KTST_TOUCH){
			PGMapSub_ToTouchChangeMark((void*)wk);
		}
		wk->sys->key_mode = APP_KTST_TOUCH;
		if(ret >= 0){
			return ret;
		}
		return PGMAP_SEQ_MARK_MAIN;
	}

	if(PGFrame_ChangeKeyModeCheck(wk->sys)){
		if(wk->sys->key_mode != wk->sys->old_key_mode){
			PGMapSub_ToKeyChangeMark((void*)wk);
#if GS_BTS5514_20090708_FIX
			Snd_SePlay(SE_PGEAR_MOVE_CURSOR);
			return PGMAP_SEQ_MARK_MAIN;
#endif
		}
	}

	switch(wk->mkedit_st){
	case MKEDIT_SEL:
		ret = PGMapKey_ContMEditTargetSelect(wk);
		break;
	case MKEDIT_MSEL:
		ret = PGMapKey_ContMEditMarkSelect(wk);
		break;
	case MKEDIT_DEL:
		ret = PGMapKey_ContMEditTargetDelete(wk);
		break;
	}
	if(ret >= 0){
		return ret;
	}
	//通常処理
	return PGMAP_SEQ_MARK_MAIN;
}

/**
 *	@brief	ポケギアマップ　マーク画面リターン
 */
static int mapseq_MarkEndMap(PGMAP_SYS_WORK* wk)
{
	if(!PGMap_MarkReturn(wk)){
		return PGMAP_SEQ_MARK_END_MAP;
	}
	return PGMAP_SEQ_MAIN;
}

/**
 *	@brief	ポケギアマップ　マーク画面リターン簡易会話呼び出し
 */
static int mapseq_MarkEndWord(PGMAP_SYS_WORK* wk)
{
	wk->sys->call_end = 0;	//呼び出しフラグリセット
	wk->sys->next_mode = PG_NEXT_WORD;
	return PGMAP_SEQ_ALL_FADEOUT;
}

/**
 *	@brief	ポケギアマップ　フェードイン
 */
static int mapseq_AllFadeIn(PGMAP_SYS_WORK* wk)
{
	int i;

	switch(wk->main_seq){
	case 0:
		WIPE_SYS_Start(WIPE_PATTERN_WMS,
				WIPE_TYPE_FADEIN,
				WIPE_TYPE_FADEIN,0x0000,COMM_BRIGHTNESS_SYNC,1,wk->heapID
				);
		//BGの表示をOnにする
		if(wk->call_mode == PGMAP_CALL_MARK){
			for(i = 0;i < 4;i++){
				GF_BGL_VisibleSet(GF_BGL_FRAME1_M+i,VISIBLE_ON);
			}
		}else{
			for(i = 0;i < 8;i++){
				GF_BGL_VisibleSet(GF_BGL_FRAME0_M+i,VISIBLE_ON);
			}
		}

		//初期パレットを転送
		PaletteTrans_AutoSet(wk->sys->palAnm,TRUE);
		SoftFadePfd(wk->sys->palAnm,FADE_MAIN_BG,16*PGAPP_PALIDX_M,16*PGAPP_PALNUM_M,0,0x0000);
		SoftFadePfd(wk->sys->palAnm,FADE_MAIN_OBJ,16*PGAPPOAM_PALIDX_M,16*PGAPPOAM_PALNUM_M,0,0x0000);
		PaletteFadeTrans( wk->sys->palAnm );
		PaletteTrans_AutoSet(wk->sys->palAnm,FALSE);

		GF_Disp_GX_VisibleControl(GX_PLANEMASK_OBJ,VISIBLE_ON);
		GF_Disp_GXS_VisibleControl(GX_PLANEMASK_OBJ,VISIBLE_ON);

		wk->main_seq++;
		break;
	case 1:
		if(!WIPE_SYS_EndCheck()){
			break;
		}
		wk->main_seq = 0;
		if(wk->call_mode == PGMAP_CALL_MARK){
			return PGMAP_SEQ_MARK_MAIN;
		}else{
			return PGMAP_SEQ_MAIN;
		}
	}

	return PGMAP_SEQ_ALL_FADEIN;
}
/**
 *	@brief	ポケギアマップ　フェードアウト
 */
static int mapseq_AllFadeOut(PGMAP_SYS_WORK* wk)
{
	int i;
	switch(wk->main_seq){
	case 0:
		WIPE_SYS_Start(WIPE_PATTERN_WMS,
				WIPE_TYPE_FADEOUT,
				WIPE_TYPE_FADEOUT,0x0000,COMM_BRIGHTNESS_SYNC,1,wk->heapID
				);
		wk->main_seq++;
		break;
	case 1:
		if(!WIPE_SYS_EndCheck()){
			break;
		}
		//BGの表示をOffにする
		for(i = 0;i < 8;i++){
			GF_BGL_VisibleSet(GF_BGL_FRAME0_M+i,VISIBLE_OFF);
		}
		GF_Disp_GX_VisibleControl(GX_PLANEMASK_OBJ,VISIBLE_OFF);
		GF_Disp_GXS_VisibleControl(GX_PLANEMASK_OBJ,VISIBLE_OFF);
		wk->main_seq = 0;
		return PGMAP_SEQ_END;
	}

	return PGMAP_SEQ_ALL_FADEOUT;
}

/**
 *	@brief	ポケギアマップ　アプリ切替フェードイン
 */
static int mapseq_ViewFadeIn(PGMAP_SYS_WORK* wk)
{
	int i;
	int flag1,flag2;

	switch(wk->main_seq){
	case 0:
		PaletteTrans_AutoSet(wk->sys->palAnm,TRUE);
		wk->palEvy = 0;

		//メインBG123の表示をOnにする
		for(i = 0;i < 3;i++){
			GF_BGL_VisibleSet(GF_BGL_FRAME1_M+i,VISIBLE_ON);
		}
		//Brightnessを解除する
		SetBrightness(BRIGHTNESS_NORMAL,
				(PLANEMASK_BG1 | PLANEMASK_BG2 | PLANEMASK_BG3),MASK_MAIN_DISPLAY);

		//サブ画面の初期スクロール値をセットする
//		PGMapSub_GuideScrollSet(wk,0);
		wk->main_seq++;
		break;
	case 1:
		flag1 = PGMapSub_PalFadeWait(wk,0);
		flag2 = PGMapSub_GuideScrollWait(wk,0);
		if(!flag1 || !flag2){
			break;
		}
		wk->main_seq++;
		break;
	case 2:
		PaletteTrans_AutoSet(wk->sys->palAnm,FALSE);
		wk->main_seq = 0;
		wk->sub_scr_end = 0;
		return PGMAP_SEQ_MAIN;
	}

	return PGMAP_SEQ_VIEW_FADEIN;
}
/**
 *	@brief	ポケギアマップ　アプリ切替フェードアウト
 */
static int mapseq_ViewFadeOut(PGMAP_SYS_WORK* wk)
{
	int i;
	int flag1,flag2;

	switch(wk->main_seq){
	case 0:
		PaletteTrans_AutoSet(wk->sys->palAnm,TRUE);

		//メインBG123の表示をOnにする
		for(i = 0;i < 3;i++){
			GF_BGL_VisibleSet(GF_BGL_FRAME1_M+i,VISIBLE_ON);
		}
		//サブ画面の初期スクロール値をセットする
		PGMapSub_GuideScrollSet(wk,1);

		//パレットフェード準備
		wk->palEvy = 0;
		wk->main_seq++;
		break;
	case 1:
		flag1 = PGMapSub_PalFadeWait(wk,1);
		flag2 = PGMapSub_GuideScrollWait(wk,1);
		if(!flag1 || !flag2){
			break;
		}
		wk->main_seq++;
		break;
	case 2:
		//アプリ用パレットを塗りつぶす
		SoftFadePfd(wk->sys->palAnm,FADE_MAIN_BG,16*PGAPP_PALIDX_M,16*PGAPP_PALNUM_M,16,0x0000);
		SoftFadePfd(wk->sys->palAnm,FADE_MAIN_OBJ,16*PGAPPOAM_PALIDX_M,16*PGAPPOAM_PALNUM_M,16,0x0000);
		PaletteFadeTrans( wk->sys->palAnm );

		//メインBG123の表示をOffにする
		for(i = 0;i < 3;i++){
			GF_BGL_VisibleSet(GF_BGL_FRAME1_M+i,VISIBLE_OFF);
		}
		PaletteTrans_AutoSet(wk->sys->palAnm,FALSE);

		wk->main_seq = 0;
		wk->sub_scr_end = 0;
		return PGMAP_SEQ_END;
	}
	return PGMAP_SEQ_VIEW_FADEOUT;
}
